package com.example.apple.weather.model;

import android.util.Log;

import com.example.apple.weather.bean.WeatherResult;
import com.example.apple.weather.iface.WeatherInterface;
import com.example.apple.weather.listener.WeatherListener;
import com.example.apple.weather.retrofitrequset.WeatherRequest;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Apple on 2017/5/14.
 */

public class Weather51Model implements WeatherInterface{
    public static Weather51Model weather51Model;
    Retrofit retrofit;
    private static final String BASE_URL="http://tianqi.2345.com/";
    public Weather51Model() {
        retrofit = new Retrofit.Builder()
                .baseUrl( BASE_URL ) //主机地址
                .addConverterFactory(GsonConverterFactory. create ())//解析方法 .
                .build();

    }

    @Override
    public void getWeather(String citynumber, final WeatherListener listener) {
        WeatherRequest request=retrofit.create(WeatherRequest.class);
        Call<WeatherResult> call=request.getReault(citynumber);
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if (response.isSuccessful()&& response.body()!=null){
                    Log.i("test",String.valueOf(response.body().getWeatherinfo().getCity()));
                    listener.onResponse(response.body().getWeatherinfo());
                }
                else{
                    listener.onFall("解析错误");
                }
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                listener.onFall(t.toString());
            }
        });

    }


    public void getWeather(String citynumber, String cityname, String tv_wd, String tv_sd, String tv_fl, String tv_fx, WeatherListener listener) {

    }
}
